﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using SoccerEvaluator.Common;
using System.Xml;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using SoccerEvaluator.Common.Interface;

namespace SoccerEvaluator.Business.FIFA
{
    /// <summary>
    /// 
    /// </summary>
    class FifaProvider : IScoresProvider
    {
        /// <summary>
        /// 
        /// </summary>
        private string _url;
        /// <summary>
        /// Lista de tablas de marcadore sobre la pagina
        /// </summary>
        private string _xpathTable;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        public FifaProvider(string url, string xpathTable)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("{0}            {1}", url, xpathTable));

            _url = url;

            _xpathTable = xpathTable;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<TemplateScoreInfo> GetScores()
        {
            List<TemplateScoreInfo> scores = new List<TemplateScoreInfo>();

            try
            {
                HtmlWeb htmlWeb = new HtmlWeb();

                // Creates an HtmlDocument object from an URL
                HtmlAgilityPack.HtmlDocument document = htmlWeb.Load(_url);

                // tabla de calendario /html/body/div[2]/div[2]/div/div[3]/table/tbody
                // tabla de resultados /html/body/div[2]/div[2]/div/div[2]/table/tbody
                // "/html/body/div[2]/div[2]/div/div[2]/table/tbody"

                HtmlNode scoreTable = document.DocumentNode.SelectSingleNode(_xpathTable);

                foreach (HtmlNode n in scoreTable.ChildNodes)
                {
                    if (n.Name == "tr")
                    {
                        TemplateScoreInfo s = new TemplateScoreInfo();
                        s.Local = n.ChildNodes[3].InnerText;
                        s.Date = DateTime.Parse(n.ChildNodes[0].InnerText);
                        s.Location = n.ChildNodes[1].InnerText.Trim();
                        s.Visitor = n.ChildNodes[5].InnerText;
                        string score = n.ChildNodes[4].InnerText;

                        //Se procesa el marcador
                        ProcessScore(s, score);

                        scores.Add(s);

                        System.Diagnostics.Trace.WriteLine(s.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }

            return scores;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <param name="score"></param>
        private void ProcessScore(TemplateScoreInfo s, string score)
        {
            try
            {
                Regex r = new Regex("[0-9][:][0-9]");

                Match m = r.Match(score);

                if (r.IsMatch(score))
                {
                    string[] values = m.Value.Split(':');
                    s.LocalGoals = int.Parse(values[0]);
                    s.VisitorGoals = int.Parse(values[1]);
                }
            }
            catch (Exception ex)
            {
                s.LocalGoals = 0;
                s.VisitorGoals = 0;

                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
        }
    }
}
